package com.talent.study.disruptor.height;

import com.lmax.disruptor.WorkHandler;
import lombok.extern.slf4j.Slf4j;

import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Author: hszhou
 * @Date: 2025/4/19 14:00
 * @Description: 如果是多消费者 那么必须实现workhandler
 */
@Slf4j
public class Consumer implements WorkHandler<Trade> {

    private String consumerId;


    private AtomicInteger count = new AtomicInteger();


    public static AtomicInteger COUNT = new AtomicInteger();


    private Random random = new Random();
    public Consumer(String consumerId){
        this.consumerId= consumerId;
    }
    @Override
    public void onEvent(Trade event) throws Exception {
        Thread.sleep(1*random.nextInt(5));

        int i = count.incrementAndGet();
        log.info("当前消费者【{}】，消费信息的id是【{}】,已经消费了【{}】条数据",this.consumerId,event.getId(),i);
        COUNT.incrementAndGet();
    }

    public int getCount(){
        return count.get();
    }
}
